#ifndef FG_MASTERNET_SERVER_MODULE_H
#define FG_MASTERNET_SERVER_MODULE_H

//#include "Common/NFMessageDefine/NFMsgDefine.h"
#include "PluginModule/IMasterNetServerModule.h"
//#include "Common/Interface/NFIKernelModule.h"
#include "PluginModule/ILogModule.h"
#include "PluginModule/INetModule.h"
#include "PluginModule/IConfigModule.h"
//#include "Common/Interface/NFIClassModule.h"
//#include "Common/Interface/NFIElementModule.h"

namespace FragrantGrass
{
	class MasterNetServerModule
		: public IMasterNetServerModule
	{
	public:
		typedef std::map<int, FG_SHARE_PTR<ServerData>> IDServerDataMap;

	public:
		MasterNetServerModule(IPluginManager* p)
		{
			m_pPluginManager = p;
			mnLastLogTime = m_pPluginManager->GetNowTime();
		}
		virtual ~MasterNetServerModule();

		virtual bool Init();
		virtual bool Shut();

		virtual bool AfterInit();
		virtual bool Execute();

		virtual void LogReceive(const char* str) {}
		virtual void LogSend(const char* str) {}

		virtual std::string GetServersStatus();

	protected:
		void OnSocketEvent(const SOCK nSockIndex, const FG_NET_EVENT eEvent, INet* pNet);
		void OnClientDisconnect(const SOCK nAddress);
		void OnClientConnected(const SOCK nAddress);

	protected:
		void OnHandleWorldServerRegiste(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnHandleWorldServerUnRegiste(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnHandleWorldServerRefresh(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);

		void OnHandleLoginServerRegiste(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnHandleLoginServerUnRegiste(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnHandleLoginServerRefresh(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);

		void OnSelectWorldProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnSelectServerResultProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);

		void OnHandleServerInfoReport(const SOCK nFd, const int msgId, const char* buffer, const uint32_t nLen);

		//////////////////////////////////////////////////////////////////////////

		void SynWorldServerInfoToLoginServerAndWorldServer();
		//printf world server and login server status ecah 60 seconds
		void LogGameServer();

		void OnHandleHeartBeat(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void InvalidMessage(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);

	private:

		INT64 mnLastLogTime;

		//serverid,data
		IDServerDataMap mMasterMap;
		IDServerDataMap mLoginMap;
		IDServerDataMap mWorldMap;
		IDServerDataMap mProxyMap;
		IDServerDataMap mGameMap;


		//NFIElementModule* m_pElementModule;
		//NFIClassModule* m_pClassModule;
		//NFIKernelModule* m_pKernelModule;
		ILogModule* m_pLogModule;
		INetModule* m_pNetModule;
		IConfigModule* m_pConfigModule;
	};
}

#endif//FG_MASTERNET_SERVER_MODULE_H